user_list = [
    {'username': 'xxm', 'password': 'xxm123', 'money': 100},
    {'username': 'xxh', 'password': 'xxh123', 'money': 0},
]
def check_user(user_name):
    """获取用户ID"""
    #print(user_name)
    for user in user_list:
        # user = {'username': 'xxm', 'password': 'xxm123', 'money': 100},
        # user =  {'username': 'xxh', 'password': 'xxh123', 'money': 0},
        # 当user_name是  xxh的时候   遍历 userlist  的字典给 user ,分别跟 user_name 对比
        if user_name == user['username']:
            # 对比成功,则找到 {'username': 'xxh', 'password': 'xxh123', 'money': 0} 这个字典
            #对应的下表,是1 ,然后在加1 ,这个加1 是自己设计上的
            #print(user_list.index(user))
            return user_list.index(user)

def check_login(user_name, pass_word):
    """验证账号密码"""
    for user in user_list:
        #user 是{'username': 'xxh', 'password': 'xxh123', 'money': 0}
        #user['username'] 就是  'xxh'
        if user_name == user['username'] and pass_word == user['password']:
            return True

def draw_money(uid):
    """取款"""
    money = input('请输入取出金额：')
    while not money.isdigit():
        money = input('输入错误，请重新输入要取出金额：')

    if int(money) <= user_list[uid]['money']:  # 取款金额小于余额

        user_list[uid]['money'] -= int(money)  # username 减款
        print('-' * 30)
        print('操作成功！此次取款：{:.2f} 元，您的余额为：{:.2f} 元'.format(int(money), user_list[uid]['money']))
        print('-' * 30)

    else:  # 取款金额大于余额
        print('-' * 30)
        print('您的余额为：{:.2f} 元，不足{:.2f}元，请重新操作！'.format(user_list[uid]['money'], int(money)))
        print('-' * 30)

def save_money(uid):
    """存款"""
    money = input('请输入存款金额：')
    while not money.isdigit():
        money = input('输入错误，请重新输入要存入金额：')
    user_list[uid]['money'] += int(money)
    print('-' * 30)
    print('操作成功！此次存入：{:.2f} 元，您的余额为：{:.2f} 元'.format(int(money), user_list[uid]['money']))
    print('-' * 30)

def zhuan_money(uid):
    """转账"""
    zhuan_user = input('请输入转款用户名：')
    zhuan_uid = check_user(zhuan_user)
    if zhuan_uid != None:  # 转账用户存在
        money = input('请输入转账金额：')
        while not money.isdigit():
            money = input('输入错误，请重新输入转账金额：')

        if int(money) <= user_list[uid]['money']:  # 转账金额小于余额
            user_list[uid]['money'] -= int(money)  # username 减款
            user_list[zhuan_uid]['money'] += int(money)  # zhuan_user 加款
            print('-' * 30)
            print('操作成功！成功向 {} 转账 {:.2f} 元，您的余额为：{:.2f} 元'.format(zhuan_user, int(money), user_list[uid]['money']))
            print('-' * 30)

        else:  # 转账金额大于余额
            print('-' * 30)
            print('您的余额为：{:.2f} 元，不足{:.2f}元，请重新操作！'.format(user_list[uid]['money'], int(money)))
            print('-' * 30)

    else:
        print('-' * 25)  # 转账用户不存在
        print('转账用户不存在，请重新操作！')
        print('-' * 25)

def main():
    """程序主函数"""
    while True:
        print('=' * 20, '欢迎来到自动取款机', '=' * 20)
        username = input('请输入你的账号：')
        uid = check_user(username)
        #print(uid)
        if uid != None:
            password = input('请输入你的密码：')
            if check_login(username, password):  # 密码正确
                #
                print('-' * 20)
                print('登陆成功！')
                print('-' * 20)
                while True:
                    print('1.查询\n2.取款\n3.存入\n4.转账\n5.登出')
                    choice = input('请输入指令：')
                    if choice == '1':  # 查询
                        print('-' * 20)
                        print('您的余额为：{:.2f} 元'.format(user_list[uid]['money']))
                        print('-' * 20)
                        # user_list = [
                        #     {'username': 'xxm', 'password': 'xxm123', 'money': 100},
                        #     1{'username': 'xxh', 'password': 'xxh123', 'money': 0},
                        # ]
                    elif choice == '2':  # 取款
                        draw_money(uid)

                    elif choice == '3':  # 存入
                        save_money(uid)

                    elif choice == '4':  # 转账
                        zhuan_money(uid)

                    elif choice == '5':  # 登出当前账号
                        print('-' * 25)
                        print('当前帐号登出成功！')
                        print('-' * 25)
                        break

                    else:  # 指令输入错误
                        print('-' * 25)
                        print('指令错误，请重新输入！')
                        print('-' * 25)

            else:  # 密码错误
                print('-' * 25)
                print('密码错误，请重新登陆！')
                print('-' * 25)

        else:  # 账号不存在
            print('-' * 25)
            print('账号不存在，请重新输入！')
            print('-' * 25)

main()
